package com.app.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamSource;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class TyUtil {


    public static final String url = "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal?keyword=";
    public static final String token = "91d8d04c-42e5-4333-9945-efda02f1cf7d";
    /**
     * 主函数
     * @param args
     */
    public static void main(String[] args) throws UnsupportedEncodingException {

        // token可以从 数据中心 -> 我的接口 中获取
        String token = "91d8d04c-42e5-4333-9945-efda02f1cf7d";
        String keyWord = "汉武御酒业销售（酒泉）有限公司";
        String url = "http://open.api.tianyancha.com/services/open/ic/baseinfo/normal?keyword="+keyWord;
        System.out.println(executeGet(url, token));
    }

    /**
     * http get请求
     * @param url 接口url
     * @param token token
     * @return  返回接口数据
     */
    public static String executeGet(String url, String token) {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 1000);
        HttpConnectionParams.setSoTimeout(httpParams, 1000);
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        String result = null;
        try {

            HttpGet get = new HttpGet(url);
            // 设置header
            get.setHeader("Authorization",token);
            // 设置类型
            HttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return result;
    }

         }
